import React, { useReducer } from 'react';

const initState = { count: 0 };
const reducer = (state, action) => {
    switch (action.type) {
    case 'increment':
        return { count: state.count + 1 };
    case 'decrement':
        return { count: state.count - 1 };
    default:
        throw new Error();
    }
};

export default function UseReducer () {
    const [state, dispatch] = useReducer(reducer, initState);
    return (
        <>
            Count: {state.count}
            <br/>
            <button onClick={() => dispatch({ type: 'decrement' })}>-</button>
            <br/>
            <button onClick={() => dispatch({ type: 'increment' })}>+</button>
        </>
    );
}
